﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLMB.Models;
using QLMB.ViewModels;
using System.Web.Script.Serialization;
using QLMB.Infrastructure;

namespace QLMB.Controllers
{
    public class FlightController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult ListFlightNow()
        {
            var info = new FlightInfo();
            info.dschuyenbay = this.Repository.ListFlightNow();
            return View(info);
        }
        [HttpGet]
        public ActionResult ListFlightPast()
        {
            var info = new FlightInfo();
            info.dschuyenbay = this.Repository.ListFlightPast();
            return View(info);
        }
        [HttpGet]
        public ActionResult GetRoute()
        {
            //var db = new InfoFlight();
            //db.Ngaydi = DateTime.Now.Date;
            //return View(db);
            var info = new RouteInfo();
            info.DSTpDi = info.DSTpDen = this.Repository.LayDSTP();
            info.Ngay = DateTime.Now.AddDays(3);
            info.TpDen = " ";
            info.TpDi = " ";
            return View(info);
        }

        [HttpPost]
        public ActionResult GetRoute(RouteInfo info, int dif = 0)
        {
            int hour = info.Gio;
            info.Ngay = info.Ngay.AddHours(hour).AddMinutes(info.Phut);
            return RedirectToAction("CreateFlight", "Flight", info);

        }

        [HttpGet]
        public ActionResult CreateFlight(RouteInfo info, int dif = 0)
        {
            //info.MaDb = this.Repository.LayMaDuongBay(info.TpDi, info.TpDen);
            List<SelectListItem> ht = this.Repository.LayHanhTrinh(info.TpDi, info.TpDen);
            info.dsSanBay = new List<FlightItem>();
            foreach (var db in ht)
            {
                info.MaDb = db.Value;
                info.LoaiHanhTrinh = db.Text;
            }
            info.thoigianbay = this.Repository.LayThoiGianBay(info.MaDb);
            if (info.LoaiHanhTrinh.CompareTo("Bay thẳng") == 0)
            {
                info.dschuyenbay = this.Repository.LayMaDB_BayThang(info.TpDi, info.TpDen);
                foreach (var db in info.dschuyenbay)
                {
                    info.MaDb = db.Value;
                    info.loaidb = db.Text;
                }
                info.dsSanBay.Add(new FlightItem(info.MaDb, info.TpDi, info.TpDen, "AIRLINE", info.thoigianbay, 0));
                info.dsSanBay.First().NgayDi = info.Ngay;
                info.dsSanBay.First().NgayDen = info.Ngay.AddMinutes((double)info.thoigianbay);
                info.DsMayBay = this.Repository.LayDSMayBay(info.loaidb, info.dsSanBay.First().NgayDi, (DateTime)info.dsSanBay.First().NgayDen);
            }
            else
            {
                //info.dschuyenbay = this.Repository.LayMaDB_HopTac(info.TpDi, info.TpDen);
                info.dsSanBay = this.Repository.LaySanBay(info.MaDb);
                info.loaidb = "Quốc tế";
                //info.DsMayBay = this.Repository.LayDSMayBay(info.loaidb);
                int i = 0;
                DateTime temp = DateTime.Now.Date;
                foreach (var sb in info.dsSanBay)
                {

                    if (i == 0)
                    {
                        sb.NgayDi = info.Ngay;
                        sb.NgayDen = info.Ngay.AddMinutes((double)sb.ThoiGianBay);
                    }
                    else
                    {
                        sb.NgayDi = temp.AddHours(3);
                        sb.NgayDen = sb.NgayDi.AddMinutes((double)sb.ThoiGianBay);
                    }
                    if (sb.HangKhaiThac == "AIRLINE")
                    {
                        sb.Maybay = 1;
                        info.DsMayBay = this.Repository.LayDSMayBay(info.loaidb, sb.NgayDi, (DateTime)sb.NgayDen);
                    }
                    temp = (DateTime)sb.NgayDen;
                    i++;

                }
            }
            TempData["FlightInfo"] = info;
            return View(info);
        }
        [HttpPost]
        public ActionResult CreateFlight(RouteInfo info)
        {
            string mb = info.MayBay;
            int khuyenmai = info.KhuyenMai;
            info = (RouteInfo)TempData["FlightInfo"];
            info.MayBay = mb;
            info.KhuyenMai = khuyenmai;
            TempData["FlightInfo"] = info;
            return RedirectToAction("Submit", "Flight", info);
        }

        [HttpGet]
        public ActionResult ViewFlight(CHUYENBAY cb)
        {
            if (cb.NgayDen != null)
            {
                FlightItem fitem = this.Repository.LayThongTinChuyenBay(cb);
                return View(fitem);
            }
            else
            {
                List<FlightItem> li_fis = this.Repository.LayThongTinChuyenBay(cb, 1);
                ViewBag.dschuyenbay = li_fis;
                return View(this.Repository.LayThongTinChuyenBay(cb));
            }

        }

        [HttpGet]
        public ActionResult EditFlight(FlightItem fitem)
        {
            string loaidb;
            if (fitem.MaCB.Contains("VN"))
                loaidb = "Nội địa";
            else loaidb = "Quốc tế";
            fitem.DsMB = this.Repository.LayDSMayBay(loaidb, DateTime.MinValue, DateTime.MinValue);
            fitem.Gio = fitem.NgayDi.Hour;
            fitem.Phut = fitem.NgayDi.Minute;
            TempData["thongtinchuyenbay"] = fitem;
            return View(fitem);
        }

        [HttpPost]
        public ActionResult EditFlight(FlightItem fitem, int dif = 1)
        {
            FlightItem old_fitem = (FlightItem)TempData["thongtinchuyenbay"];
            string MaNV = "NV01";
            string MaCB = fitem.MaCB;
            DateTime now = DateTime.Now;
            fitem.NgayDi = fitem.NgayDi.Date;
            fitem.NgayDi = fitem.NgayDi.AddHours((double)fitem.Gio).AddMinutes(fitem.Phut);
            old_fitem.LyDoCapNhat = fitem.LyDoCapNhat;
            string lydocapnhat = fitem.LyDoCapNhat;
            DateTime? dt = fitem.NgayDi;
            #region Cập nhật log cho việc thay đổi chuyến bay
            //if (fitem.MaMB != old_fitem.MaMB)
            //{
            //    if (fitem.NgayDi != old_fitem.NgayDi)
            //    {
            //        if (fitem.NgayDi <= DateTime.Now)
            //        {
            //            old_fitem.errmes = "Thời gian bạn chọn sớm hơn thời gian hiện tại";
            //            return RedirectToAction("EditFlight", old_fitem);
            //        }
            //        fitem.NgayDen = fitem.NgayDi.AddMinutes((double)fitem.ThoiGianBay);
            //        if (!this.Repository.KiemTraMB(fitem.MaMB, fitem.NgayDi, (DateTime)fitem.NgayDen))
            //        {
            //            old_fitem.errmes = "Máy bay " + fitem.MaMB + " không thể bay vào lúc đó";
            //            return RedirectToAction("EditFlight", old_fitem);
            //        }
            //        if (fitem.TrangThaiCB != old_fitem.TrangThaiCB)
            //        {
            //            this.Repository.CapNhatChuyenBayLog(old_fitem.TrangThaiCB, old_fitem.MaMB, old_fitem.NgayDi, MaNV, MaCB, fitem.LyDoCapNhat, now);
            //        }
            //        else
            //        {
            //            this.Repository.CapNhatChuyenBayLog(old_fitem.MaMB, old_fitem.NgayDi, MaNV, MaCB, fitem.LyDoCapNhat, now, 1);
            //        }
            //    }
            //    else
            //    {
            //        if (!this.Repository.KiemTraMB(fitem.MaMB, old_fitem.NgayDi, (DateTime)old_fitem.NgayDen))
            //        {
            //            old_fitem.errmes = "Máy bay " + fitem.MaMB + " không thể bay vào lúc đó";
            //            return RedirectToAction("EditFlight", old_fitem);
            //        }
            //        if (fitem.TrangThaiCB != old_fitem.TrangThaiCB)
            //        {
            //            this.Repository.CapNhatChuyenBayLog(old_fitem.MaMB, old_fitem.TrangThaiCB, MaNV, MaCB, fitem.LyDoCapNhat, now);
            //        }
            //        else
            //        {
            //            this.Repository.CapNhatChuyenBayLog(old_fitem.MaMB, MaNV, MaCB, fitem.LyDoCapNhat, now, 1);
            //        }
            //    }
            //}
            //else
            //{
            //    if (fitem.NgayDi != old_fitem.NgayDi)
            //    {
            //        fitem.NgayDen = fitem.NgayDi.AddMinutes((double)fitem.ThoiGianBay);
            //        if (fitem.TrangThaiCB != old_fitem.TrangThaiCB)
            //        {
            //            this.Repository.CapNhatChuyenBayLog(old_fitem.TrangThaiCB, old_fitem.NgayDi, MaNV, MaCB, fitem.LyDoCapNhat, now);
            //        }
            //        else
            //        {
            //            this.Repository.CapNhatChuyenBayLog(old_fitem.NgayDi, MaNV, MaCB, fitem.LyDoCapNhat, now);
            //        }
            //    }
            //    else
            //    {
            //        if (fitem.TrangThaiCB != old_fitem.TrangThaiCB)
            //        {
            //            this.Repository.CapNhatChuyenBayLog(old_fitem.TrangThaiCB, MaNV, MaCB, fitem.LyDoCapNhat, now);
            //        }
            //    }
            //}
            #endregion
            // kiểm tra sự thay đổi của các thuộ tính
            if ((fitem.MaMB == old_fitem.MaMB))
            {
                old_fitem.MaMB = null;
            }
            else
            {
                if (!this.Repository.KiemTraMB(fitem.MaMB, old_fitem.NgayDi, (DateTime)old_fitem.NgayDen))
                {
                    old_fitem.errmes = "Máy bay " + fitem.MaMB + " không thể bay vào lúc đó";
                    return RedirectToAction("EditFlight", old_fitem);
                }
            }
            if (dt == old_fitem.NgayDi)
            {
                dt = null;
            }
            else
            {
                if (dt <= DateTime.Now)
                old_fitem.errmes = "Thời gian bạn chọn sớm hơn thời gian hiện tại";
                return RedirectToAction("EditFlight", old_fitem);
            }
            if(fitem.KhuyenMai == old_fitem.KhuyenMai)
            {
                old_fitem.KhuyenMai = null;
            }
            if (fitem.TrangThaiCB == old_fitem.TrangThaiCB)
            {
                old_fitem.TrangThaiCB = null;
            }
            this.Repository.CapNhatChuyenBayLog(old_fitem.KhuyenMai, old_fitem.TrangThaiCB, old_fitem.MaMB, dt, MaNV, MaCB, fitem.LyDoCapNhat, now);
            return RedirectToAction("UpdateChuyenBay", fitem);
        }
       
        [HttpGet]
        public ActionResult UpdateChuyenBay(FlightItem fitem)
        {
            if (this.Repository.CapNhatChuyenBay(fitem))
                return View();
            else
            {
                fitem.errmes = "Có lỗi rồi";
                return RedirectToAction("EditFlight", fitem);
            }
        }
        [HttpGet]
        public ActionResult Submit(RouteInfo info)
        {
            info = (RouteInfo)TempData["FlightInfo"];
            if (info.dsSanBay.Count == 1) this.Repository.CreateFlight(info.dsSanBay.First(), info.MayBay, 0,info.KhuyenMai);
            else
            {
                int num = 1;
                string macbchinh = this.Repository.CreateFlight(info.Ngay, info.MaDb, info.KhuyenMai);
                foreach (var cb in info.dsSanBay)
                {
                    this.Repository.CreateFlight(cb, info.MayBay, 1, macbchinh, num);
                    num++;
                }
            }
            return View();
        }

        public JsonResult GetListArriveAirport(string id)
        {
            RouteInfo temp = new RouteInfo();
            var TpDen = this.Repository.LayDSTPDen(id);
            return Json(TpDen, JsonRequestBehavior.AllowGet);
        }
    }
}
